﻿using System.IO;
using System.Windows;
using InstantSpine.GUI.Properties;
using System.ComponentModel;
using InstantSpine.Core;

namespace InstantSpine.GUI
{
    /// <summary>
    /// Interaction logic for OptionsDialog.xaml
    /// </summary>
    public partial class OptionsDialog : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public bool OptionsAreValid {
            get {
                return !string.IsNullOrEmpty(Settings.Default.pathToAnsys) &&
                       !string.IsNullOrEmpty(Settings.Default.pathToParaSpine) &&
                       !string.IsNullOrEmpty(Settings.Default.ansysProductString) &&
                       Directory.Exists(Settings.Default.pathToParaSpine) &&
                       File.Exists(Settings.Default.pathToAnsys);
            }
        }

        public OptionsDialog() {
            InitializeComponent();
            this.DataContext = this;
        }

        private void btnSaveOptions_Click(object sender, RoutedEventArgs e) {
            this.DialogResult = true;
        }

        private void btnBrowseForAnsys_Click(object sender, RoutedEventArgs e) {
            var fileOpenDialog = new Microsoft.Win32.OpenFileDialog();
            fileOpenDialog.AddExtension = true;
            fileOpenDialog.CheckFileExists = true;
            fileOpenDialog.CheckPathExists = true;
            fileOpenDialog.ShowReadOnly = true;
            fileOpenDialog.ValidateNames = true;
            fileOpenDialog.Title = "Browse for ANSYS binary";
            fileOpenDialog.Filter = "ANSYS binary|*.exe";

            var result = fileOpenDialog.ShowDialog();

            if (result.HasValue && result.Value && !string.IsNullOrEmpty(fileOpenDialog.FileName)) {
                Settings.Default.pathToAnsys = fileOpenDialog.FileName;
            }
        }

        private void btnBrowseForParaspine_Click(object sender, RoutedEventArgs e) {
            var fileOpenDialog = new Microsoft.Win32.OpenFileDialog();
            fileOpenDialog.AddExtension = true;
            fileOpenDialog.CheckFileExists = true;
            fileOpenDialog.CheckPathExists = true;
            fileOpenDialog.ShowReadOnly = true;
            fileOpenDialog.ValidateNames = true;
            fileOpenDialog.Title = "Browse for ParaSpine";
            fileOpenDialog.Filter = "ParaSpine|para-spine.inp";

            var result = fileOpenDialog.ShowDialog();

            if (result.HasValue && result.Value && !string.IsNullOrEmpty(fileOpenDialog.FileName)) {
                Settings.Default.pathToParaSpine = Path.GetDirectoryName(fileOpenDialog.FileName);
            }
        }

        private void TextBox_TextChanged_1(object sender, System.Windows.Controls.TextChangedEventArgs e) {
            PropertyChanged.Raise(() => OptionsAreValid);
        }

        private void TextBox_TextChanged_2(object sender, System.Windows.Controls.TextChangedEventArgs e) {
            PropertyChanged.Raise(() => OptionsAreValid);
        }

        private void TextBox_TextChanged_3(object sender, System.Windows.Controls.TextChangedEventArgs e) {
            PropertyChanged.Raise(() => OptionsAreValid);
        }
    }
}
